#motologger.py

#Get Data First
log_name = 'motologger_log0116.txt'
log_file = open(log_name,'r')
header_length = len(log_file.readline())
log_file.close()
log_file = open(log_name,'r')
log_txt = log_file.read()
#print log_txt
rpm_tag = 'r'
GPS_tag = 'g'
vel_tag = 'v'
acc_tag = 'a'
batt_tag = 'b'
gear_tag = 'e'


tags = []
tags.append(gear_tag)
tags.append(vel_tag)
tags.append(rpm_tag)
tags.append(acc_tag)

#parse header
m_header = []

for i in range(0,len(tags)):
    #print i
    m_header.append([])
    sample_rate = 0
    for j in range(0,64):
        if log_txt[j] == tags[i]:
            j=j+2
            for k in range(1,8):
                if log_txt[j+k]==',' or log_txt[j+k]=='\n':
                    sample_rate = log_txt[j:j+k]
                    #print sample_rate
                    break
            break
    m_header[i].append((tags[i],sample_rate))


#parse data, note:gps requires a seperate function
def read_data_points(tag, header_length):
    data_points = []
    time_stamp_points = []
    if tag == GPS_tag:
        data_points, time_stamp_points = read_GPS_points(header_length)
    else:
        for i in range(header_length,len(log_txt)-2):
            if log_txt[i]==tag:
                i = i+2
                j=1
                while (log_txt[i+j] != ',') and (log_txt[i+j] != '\n'):
                    #print log_txt[i+j]
                    j = j+1
                time_stamp = log_txt[i:i+j]
                #print "time+stamp=", time_stamp
                time_stamp_points.append(time_stamp)
                i=i+j+1
                j=1
                while log_txt[i+j] != ',' and log_txt[i+j] != '\n':
                    j = j+1
                data = log_txt[i:i+j]
                i=i+j+1
                data_points.append(data)
    return data_points, time_stamp_points
#read GPS points; data_points=2D lat,long;time_points=UTC Time
#North positive; East positive
def read_GPS_points(header_length):
    data_points = []
    time_points = []
    data1=[]
    data2=[]
    for i in range(header_length,len(log_txt)-2):
            if log_txt[i-1] == 'G' and log_txt[i]=='A':
                i = i+2
                j=1
                while (log_txt[i+j] != ',') and (log_txt[i+j] != '\n'):
                    #print log_txt[i+j]
                    j = j+1
                time_stamp = log_txt[i:i+j]
                #print "time+stamp=", time_stamp
                time_points.append(time_stamp)
                #look at the dumb way i parsed this!! wowsers
                i=i+j+1
                j=1
                k=1
                l=1
                m=1
                while log_txt[i+j] != ',' and log_txt[i+j] != '\n':
                    j = j+1
                while log_txt[i+j+k] != ',' and log_txt[i+j+k] != '\n':
                    k = k+1
                while log_txt[i+j+k+l] != ',' and log_txt[i+j+k+l] != '\n':
                    l = l+1
                while log_txt[i+j+k+l+m] != ',' and log_txt[i+j+k+l+m] != '\n':
                    m = m+1
                #end amazing parsing

                #GPS Conversion
                data1 = int(float(log_txt[i:i+j])/100)+((float(float(log_txt[i:i+j])/(100))-int(float(log_txt[i:i+j])/100)))*100/60
                data2 = int(float(log_txt[i+j+k+1:i+j+k+l])/100)+((float(float(log_txt[i+j+k+1:i+j+k+l])/(100))-int(float(log_txt[i+j+k+1:i+j+k+l])/100)))*100/60
                data = (str(data1),str(data2))

                i=i+j+k+1

                data_points.append(data)
    return data_points, time_points

#get each data set
vel_points,vel_times = read_data_points(vel_tag,header_length)
GPS_points,GPS_times = read_data_points(GPS_tag,header_length)
rpm_points, rpm_times = read_data_points(rpm_tag,header_length)
gear_points, gear_times = read_data_points(gear_tag,header_length)
batt_points, batt_times = read_data_points(batt_tag,header_length)
acc_points, acc_times = read_data_points(acc_tag,header_length)

#print GPS_points
#print GPS_points[0][1]
log_file.close()


#Now make the xml files
#adapted from http://www.boddie.org.uk/python/XML_intro.html
import xml.dom.minidom
#dir(xml.dom.minidom) list contents of module

#create a new xml doc
def get_a_document():
    doc = xml.dom.minidom.Document()
    markers_element = doc.createElementNS("motologger","markers")#add markers as root element with namespace motologger
    doc.appendChild(markers_element) #adds to markers_element to document
    return doc, markers_element

def add_a_line(doc, markers_element):
    line_element = doc.createElementNS("motologger","line")
    markers_element.appendChild(line_element)
    return line_element

def add_a_point(doc, line_element):
    point_element = doc.createElementNS("motologger","point")
    line_element.appendChild(point_element)
    return point_element

doc,markers_element = get_a_document()

line_element = []
point_element = []
color_num = []
color = []

from numpy import *
#from:http://mail.python.org/pipermail/tutor/2003-November/026136.html
def str2num(s):
    return ("." in s and [float(s)] or [int(s)])[0]
gear_ints = map(str2num, gear_points)

#min_gear = min((gear_ints))
#max_gear = max((gear_ints))

min_gear = gear_ints[0]
max_gear = gear_ints[0]

for counter in range(2,len(gear_ints)-2):
    if sort(gear_ints[counter-2:counter+3])[2]<min_gear:
        min_gear = sort(gear_ints[counter-2:counter+3])[2]
    if sort(gear_ints[counter-2:counter+3])[2]>max_gear:
        max_gear = sort(gear_ints[counter-2:counter+3])[2]

gear_range = max_gear - min_gear
if gear_range >0:
    gear_map = float(510/gear_range)
else:
    gear_map = 1
gear_ratio = len(gear_points)/(len(GPS_points)-1)
#blue_rat =
for i in range(0,len(GPS_points)-1):
    r = "00" #init RGB values
    g = "00"
    b = "00"
    color.append([])
    color_num.append([])
    #color_num[i]=int(gear_map*(sum(gear_ints[i*gear_ratio:i*gear_ratio+gear_ratio])-min_gear*gear_ratio)/gear_ratio)
    color_num[i]=int(gear_map*int(sort(gear_ints[i*gear_ratio:i*gear_ratio+gear_ratio])[gear_ratio/2]))-min_gear
    if color_num[i] > 255:
        r= hex(color_num[i]-255)[2:4]
        if len(r) == 1:
            r= "0"+r
    if color_num[i] > 127 and color_num[i]<382:
        g = hex(color_num[i]-128)[2:4]
        if len(g) == 1:
            g= "0"+g
    if color_num[i] < 255:
        b= hex(255-color_num[i])[2:4]
        if len(b) == 1:
            b= "0"+b
#    r = hex(color_num[i]-255)[2:3]
#    g = hex()
    line_element.append([])
    line_element[i] = add_a_line(doc, markers_element)
    line_element[i].setAttribute("colour","#"+r+g+b)
    line_element[i].setAttribute("width","4")
    line_element[i].setAttribute("html","123")

    point_element.append([])
    point_element.append([])
    point_element[2*i] = add_a_point(doc, line_element[i])
    point_element[2*i].setAttribute("lat",GPS_points[i][0])
    point_element[2*i].setAttribute("lng","-"+GPS_points[i][1])
    point_element[2*i+1] = add_a_point(doc, line_element[i])
    point_element[2*i+1].setAttribute("lat",GPS_points[i+1][0])
    point_element[2*i+1].setAttribute("lng","-"+GPS_points[i+1][1])
##    point_element[2*i].setAttribute("lng","-79.0")
##    point_element[2*i+1] = add_a_point(doc, line_element[0])
##    point_element[2*i+1].setAttribute("lat","43.8")
##    point_element[2*i+1].setAttribute("lng","-79.0")
##line_element.append([])
##line_element[0] = add_a_line(doc, markers_element)
##
##line_element[0].setAttribute("colour","#0000FF")
##line_element[0].setAttribute("width","4")
##line_element[0].setAttribute("html","123")
###line_element = add_a_line(doc,markers_element)
##
##
##point_element.append([])
##point_element[0] = add_a_point(doc, line_element[0])
##
##point_element[0].setAttribute("lat","43.8")
##point_element[0].setAttribute("lng","-79.0")
##
##point_element.append([])
##point_element[1] = add_a_point(doc, line_element[0])
##
##point_element[1].setAttribute("lat","45.8")
##point_element[1].setAttribute("lng","-79.0")

xml_file = open('motologger/gear.xml','w')

doc.writexml(xml_file, "", "    ", "\n", "UTF-8")

xml_file.close()